﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class WaveDetailBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private static readonly IOrdersDA orderDa = DataAccess.CreateOrdersDA();
        private static readonly IOrderDetailDA orderDetailDA = DataAccess.CreateOrderDetailDA();
        private PubParasInfo pubParasInfo;
        private static readonly IWaveDA waveDa = DataAccess.CreateWaveDA();
        private static readonly IWaveDetailDA waveDetailDa = DataAccess.CreateWaveDetailDA();

        public WaveDetailBLL()
        {
            this.pubParasInfo = null;
        }

        public WaveDetailBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public WaveDetailBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteWave(string waveID, List<string> IDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<WaveDetailInfo> waveDetailInfos = waveDetailDa.GetWaveDetailListByID(dataBase, tran, waveID, this.dataBaseID[0]);
                if (waveDetailDa.DeleteWaveDetail(dataBase, tran, waveID, IDS, this.dataBaseID[0]) > 0)
                {
                    for (int i = 0; i < waveDetailInfos.Count; i++)
                    {
                        if (waveDetailInfos[i].OrderLineID == 0)
                        {
                            orderDa.UpdateOrderBatchFlag(dataBase, tran, "N", IDS[i], this.dataBaseID[0]);
                            break;
                        }
                        orderDetailDA.UpdateOrderDetailBatchFlag(dataBase, tran, "N", waveDetailInfos[i].OrderID, waveDetailInfos[i].OrderLineID.ToString(), this.dataBaseID[0]);
                        temp = temp + IDS[i] + ",";
                    }
                    if ((waveDetailInfos != null) && (waveDetailInfos.Count > 0))
                    {
                        string waveStatus = new WaveBLL(this.pubParasInfo).GetWaveStatus(waveDetailInfos);
                        waveDa.UpdateWaveStatus(dataBase, tran, waveID, waveStatus, this.pubParasInfo.UserID, this.dataBaseID[0]);
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllWaveDetailInfo()
        {
            return waveDetailDa.GetAllWaveDetail(this.dataBaseID[0]);
        }

        public string InsertWaveDetail(WaveDetailInfo obj)
        {
            string waveDetailID = string.Empty;
            try
            {
                if (obj == null)
                {
                    return waveDetailID;
                }
                DataBase dataBase = new DataBase();
                DbConnection conn = dataBase.connection;
                conn.Open();
                DbTransaction tran = conn.BeginTransaction();
                try
                {
                    try
                    {
                        if (waveDetailDa.CheckWaveDetailIDUnique(dataBase, tran, obj.WaveID, obj.OrderID.ToString(), this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F7300_002", this.pubParasInfo.UserLanguage, new List<string> { obj.WaveID, obj.OrderID.ToString() });
                        }
                        obj.UpdatedBy = obj.CreatedBy;
                        obj.CreatedDate = DateTime.Now;
                        obj.UpdatedDate = DateTime.Now;
                        if (waveDetailDa.InsertWaveDetail(dataBase, tran, obj, this.dataBaseID[0]) > 0)
                        {
                            waveDetailID = obj.OrderID.ToString();
                            orderDa.UpdateOrderBatchFlag(dataBase, tran, "Y", obj.OrderID, this.dataBaseID[0]);
                            new WaveBLL(this.pubParasInfo).UpdateWaveStatusByOrderID(dataBase, tran, obj.OrderID);
                            tran.Commit();
                        }
                    }
                    catch
                    {
                        tran.Rollback();
                    }
                    return waveDetailID;
                }
                finally
                {
                    conn.Close();
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return waveDetailID;
        }

        public string SaveWavebyOrderSelection(WaveDetailInfo waveDetail, List<string> Ids, List<string> Status)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strWaveID = string.Empty;
            try
            {
                for (int i = 0; Ids.Count > i; i++)
                {
                    WaveDetailInfo wavedetail = waveDetail;
                    wavedetail.OrderID = Ids[i];
                    wavedetail.ProcessStatus = Status[i];
                    wavedetail.UpdatedBy = waveDetail.CreatedBy;
                    if (!waveDetailDa.CheckWaveDetailIDUnique(dataBase, tran, wavedetail.WaveID, wavedetail.OrderID, this.dataBaseID[0]) && (waveDetailDa.InsertWaveDetail(dataBase, tran, wavedetail, this.dataBaseID[0]) > 0))
                    {
                        strWaveID = waveDetail.WaveID;
                        orderDa.UpdateOrderBatchFlag(dataBase, tran, "Y", wavedetail.OrderID, this.dataBaseID[0]);
                    }
                }
                new WaveBLL(this.pubParasInfo).UpdateWaveStatusByOrderID(dataBase, tran, Ids[0]);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strWaveID;
        }

        public string UpdateWaveDetail(WaveDetailInfo obj)
        {
            string waveDetailID = string.Empty;
            try
            {
                if (waveDetailDa.UpdateWaveDetail(obj, this.dataBaseID[0]) > 0)
                {
                    waveDetailID = obj.WaveID + " , " + obj.OrderID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return waveDetailID;
        }
    }
}

